1
/****************************** Module Header ******************************\
2 * Module Name: SharedSessionModule.cs
3 * Project: CSASPNETShareSessionBetweenSubDomains
4 * Copyright (c) Microsoft Corporation
6 * This project demonstrates how to configure a SQL Server as SessionState and
7 * make a module to share Session between two Web Sites with the same root domain.
9 * SharedSessionModule is used to make Web Sites use the same Application Id and
10 * Session Id to achieve sharing Session.
12 * This source is subject to the Microsoft Public License.
13 * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
14 * All other rights reserved.
16 \*****************************************************************************/
20 using System
.Reflection
;
21 using System
.Configuration
;
23 namespace CSASPNETShareSessionBetweenSubDomainsModule
26 /// A HttpModule used for sharing the session between Applications in
29 public class SharedSessionModule
: IHttpModule
31 // Cache settings on memory.
32 protected static string applicationName
= ConfigurationManager
.AppSettings
["ApplicationName"];
33 protected static string rootDomain
= ConfigurationManager
.AppSettings
["RootDomain"];
35 #region IHttpModule Members
37 /// Initializes a module and prepares it to handle requests.
39 /// <param name="context">
40 /// An System.Web.HttpApplication
41 /// that provides access to the methods,
42 /// properties, and events common to all application objects within
43 /// an ASP.NET application.
45 public void Init(HttpApplication context
)
47 // This module requires both Application Name and Root Domain to work.
48 if (string.IsNullOrEmpty(applicationName
) ||
49 string.IsNullOrEmpty(rootDomain
))
54 // Change the Application Name in runtime.
55 FieldInfo runtimeInfo
= typeof(HttpRuntime
).GetField("_theRuntime",
56 BindingFlags
.Static
| BindingFlags
.NonPublic
);
57 HttpRuntime theRuntime
= (HttpRuntime
)runtimeInfo
.GetValue(null);
58 FieldInfo appNameInfo
= typeof(HttpRuntime
).GetField("_appDomainAppId",
59 BindingFlags
.Instance
| BindingFlags
.NonPublic
);
61 appNameInfo
.SetValue(theRuntime
, applicationName
);
64 context
.PostRequestHandlerExecute
+= new EventHandler(context_PostRequestHandlerExecute
);
68 /// Disposes of the resources (other than memory) used by the module
77 /// Before sending response content to client, change the Cookie to Root Domain
78 /// and store current Session Id.
80 /// <param name="sender">
81 /// An instance of System.Web.HttpApplication that provides access to
82 /// the methods, properties, and events common to all application
83 /// objects within an ASP.NET application.
85 void context_PostRequestHandlerExecute(object sender
, EventArgs e
)
87 HttpApplication context
= (HttpApplication
)sender
;
89 // ASP.NET store a Session Id in cookie to specify current Session.
90 HttpCookie cookie
= context
.Response
.Cookies
["ASP.NET_SessionId"];
92 if (context
.Session
!= null &&
93 !string.IsNullOrEmpty(context
.Session
.SessionID
))
95 // Need to store current Session Id during every request.
96 cookie
.Value
= context
.Session
.SessionID
;
98 // All Applications use one root domain to store this Cookie
99 // So that it can be shared.
100 if (rootDomain
!= "localhost")
102 cookie
.Domain
= rootDomain
;
105 // All Virtual Applications and Folders share this Cookie too.